<?php

namespace app\bbs\controller;

use think\Controller;
use think\Db;
use think\Session;

class Articlelist extends Controller
{
    public function index()
    {
        $user_info  = Session::get('bbs_user_info');
        if ($user_info) {
            $me_permission_to_read = $user_info['user_jurisdiction'];
        } else {
            $me_permission_to_read = 0;
        }
        $_page = input('page', 1);
        $alias = input('alias', null);
        $class_id = input('class_id', 0);
        if ($alias) {
            $bbs_settings = Db::table('bbs_settings')
                ->where(['id' => 1])
                ->field('articlelist_list_number')
                ->find();
            $bbs_classification_son_id = Db::table('bbs_classification_son')
                ->where("son_class_alias='$alias'")
                ->field('id,son_class_name,main_class_id,son_class_release,son_class_start')
                ->find();
            if (!$bbs_classification_son_id) {
                $Data['code'] = '10001';
                $Data['ErrorPosition'] = 'AliasError';
                $Data['msg'] = '分类不存在';
                return json($Data);
            }
            if ($bbs_classification_son_id && $bbs_classification_son_id['son_class_start'] == 1) {
                $Data['code'] = '10001';
                $Data['ErrorPosition'] = 'AliasError';
                $Data['msg'] = '分类不存在';
                return json($Data);
            }
            $bbs_article_class_list = Db::table('bbs_article_class')
                ->where("bbs_classification_son_id = $bbs_classification_son_id[id] and start = 0")
                ->select();
            if ($bbs_classification_son_id) {
                $head = head(null, null, null, ['start' => 'layui-this', 'class_name' => $bbs_classification_son_id['son_class_name'], 'id' => $bbs_classification_son_id['main_class_id']]);
                if ($class_id) {
                    $list_count = Db::table('bbs_article')
                        ->alias('bbs_article')
                        ->join('bbs_classification_son', "son_class_alias='$alias'")
                        ->order(['bbs_article.top desc', 'bbs_article.id desc', 'bbs_article.top_time desc'])
                        ->whereor('bbs_article.top_time', 'gt', time())
                        ->whereor('bbs_article.top_time', 'eq', null)
                        ->where("bbs_article.class_id = $bbs_classification_son_id[id]") //要求分类id 和分类别民的id 一致
                        ->where("bbs_article.start = 0 OR bbs_article.start = 3 and bbs_article.permission_to_read<=$me_permission_to_read") //阅读权限
                        ->where("bbs_article.Classification_main = $class_id") //要求分类id 和分类别民的id 一致
                        ->join('user', 'bbs_article.user_id=user.id')
                        ->count();
                } else {
                    // dump($bbs_classification_son_id);
                    // return;
                    $list_count = Db::table('bbs_article')
                        ->alias('bbs_article')
                        ->join('bbs_classification_son', "son_class_alias='$alias'")
                        ->order(['bbs_article.top desc', 'bbs_article.id desc', 'bbs_article.top_time desc'])
                        ->whereor('bbs_article.top_time', 'gt', time())
                        ->whereor('bbs_article.top_time', 'eq', null)
                        ->where("bbs_article.class_id = $bbs_classification_son_id[id]") //要求分类id 和分类别民的id 一致
                        ->where("bbs_article.start = 0 and bbs_article.permission_to_read<=$me_permission_to_read") //阅读权限
                        ->join('user', 'bbs_article.user_id=user.id')
                        ->count();
                }
                if ($list_count == 0) {
                    $list_count = 1;
                }
                if ($_page > ceil($list_count / $bbs_settings['articlelist_list_number'])) {
                    $_page = ceil($list_count / $bbs_settings['articlelist_list_number']);
                } else if ($_page < 1) {
                    $_page = 1;
                }
                if ($class_id) {
                    $list = Db::table('bbs_article')
                        ->alias('bbs_article')
                        ->join('bbs_classification_son', "son_class_alias='$alias'")
                        ->order(['bbs_article.top desc', 'bbs_article.id desc', 'bbs_article.top_time desc'])
                        ->whereor('bbs_article.top_time', 'gt', time())
                        ->whereor('bbs_article.top_time', 'eq', null)
                        ->where("bbs_article.start = 0 OR bbs_article.start = 3 and bbs_article.permission_to_read<=$me_permission_to_read") //阅读权限
                        ->where("bbs_article.class_id = $bbs_classification_son_id[id]") //要求分类id 和分类别民的id 一致
                        ->where("bbs_article.Classification_main = $class_id") //要求分类id 和分类别民的id 一致
                        ->join('user', 'bbs_article.user_id=user.id')
                        ->limit($_page * $bbs_settings['articlelist_list_number'] - $bbs_settings['articlelist_list_number'], $bbs_settings['articlelist_list_number'])
                        ->field('user.user_name,user.email,user.head_src,bbs_article.top,bbs_article.start,bbs_article.title,bbs_article.id,bbs_article.content,bbs_article.release_time,user.user_name,user.nickname')
                        ->select();
                } else {
                    $list = Db::table('bbs_article')
                        ->alias('bbs_article')
                        ->join('bbs_classification_son', "son_class_alias='$alias'")
                        ->order(['bbs_article.top desc', 'bbs_article.id desc', 'bbs_article.top_time desc'])
                        ->whereor('bbs_article.top_time', 'gt', time())
                        ->whereor('bbs_article.top_time', 'eq', null)
                        ->where("bbs_article.class_id = $bbs_classification_son_id[id]") //要求分类id 和分类别民的id 一致
                        ->where("bbs_article.start = 0 OR bbs_article.start = 3 and bbs_article.permission_to_read<=$me_permission_to_read") //阅读权限
                        ->join('user', 'bbs_article.user_id=user.id')
                        ->limit($_page * $bbs_settings['articlelist_list_number'] - $bbs_settings['articlelist_list_number'], $bbs_settings['articlelist_list_number'])
                        ->field('user.user_name,user.email,user.head_src,bbs_article.top,bbs_article.start,bbs_article.title,bbs_article.id,bbs_article.content,bbs_article.release_time,user.user_name,user.nickname')
                        ->select();
                }




                foreach ($list as $key => $value) {
                    $list[$key]['count'] = Db::table('bbs_comments')
                        ->where(['article_id' => $value['id']])
                        ->count();
                }

                $page = getPageHtml($list_count, $bbs_settings['articlelist_list_number'], $_page, ['alias' => $alias, 'class_id' => $class_id]);
                return $this->fetch('articlelist', ['head' => $head, 'list' => $list, 'alias' => $alias, 'bbs_article_class_list' => $bbs_article_class_list, 'page' => $page, 'bbs_classification_info' => $bbs_classification_son_id, 'class_id' => $class_id]);
            } else {
                $Data['code'] = '10001';
                $Data['ErrorPosition'] = 'AliasError';
                return json_encode($Data);
            }
        } else {
            $Data['code'] = '10000';
            $Data['ErrorPosition'] = 'AliasError';
            return json_encode($Data);
        }
    }
    public function get_class_list()
    {
        $bbs_settings = Db::table('bbs_settings')
            ->where(['id' => 1])
            ->field('articlelist_list_number')
            ->find();
        $alias = input('alias', null);
        $class_id = input('class_id', 0);
        $bbs_classification_son_id = Db::table('bbs_classification_son')
            ->where("son_class_alias='$alias'")
            ->field('id,son_class_name,main_class_id,son_class_release,son_class_start')
            ->find();
        if (!$bbs_classification_son_id) {
            $Data['code'] = '10001';
            $Data['ErrorPosition'] = 'AliasError';
            $Data['msg'] = '分类不存在';
            return json($Data);
        }
        if ($bbs_classification_son_id && $bbs_classification_son_id['son_class_start'] == 1) {
            $Data['code'] = '10001';
            $Data['ErrorPosition'] = 'AliasError';
            $Data['msg'] = '分类不存在';
            return json($Data);
        }
        if ($class_id == 0) {
            $list = Db::table('bbs_article')
                ->alias('bbs_article')
                ->order(['bbs_article.top desc', 'bbs_article.id desc', 'bbs_article.top_time desc'])
                ->whereor('bbs_article.top_time', 'gt', time())
                ->whereor('bbs_article.top_time', 'eq', null)
                ->join('user', 'bbs_article.user_id=user.id')
                ->where("bbs_article.class_id = $bbs_classification_son_id[id]") //要求分类id 和分类别民的id 一致

                ->where("bbs_article.start = 0 OR bbs_article.start = 3") //阅读权限
                ->field('bbs_article.id,bbs_article.top,bbs_article.title,user.head_src,bbs_article.start,bbs_article.release_time,user.user_jurisdiction,user.user_name,user.nickname')
                ->limit($bbs_settings['articlelist_list_number'])
                ->select();
            $list_count = Db::table('bbs_article')
                ->alias('bbs_article')
                ->order(['bbs_article.top desc', 'bbs_article.id desc', 'bbs_article.top_time desc'])
                ->whereor('bbs_article.top_time', 'gt', time())
                ->whereor('bbs_article.top_time', 'eq', null)
                ->join('user', 'bbs_article.user_id=user.id')
                ->where("bbs_article.class_id = $bbs_classification_son_id[id]") //要求分类id 和分类别民的id 一致

                ->where("bbs_article.start = 0 OR bbs_article.start = 3") //阅读权限
                ->count();
        } else {
            $list = Db::table('bbs_article')
                ->alias('bbs_article')
                ->order(['bbs_article.top desc', 'bbs_article.id desc', 'bbs_article.top_time desc'])
                ->whereor('bbs_article.top_time', 'gt', time())
                ->whereor('bbs_article.top_time', 'eq', null)
                ->where("bbs_article.classification_main=$class_id")
                ->where("bbs_article.class_id = $bbs_classification_son_id[id]") //要求分类id 和分类别民的id 一致

                ->where("bbs_article.start = 0 OR bbs_article.start = 3") //阅读权限
                ->join('user', 'bbs_article.user_id=user.id')
                ->field('bbs_article.id,bbs_article.top,bbs_article.title,user.head_src,bbs_article.start,bbs_article.release_time,user.user_jurisdiction,user.user_name,user.nickname')
                ->limit($bbs_settings['articlelist_list_number'])
                ->select();
            $list_count = Db::table('bbs_article')
                ->alias('bbs_article')
                ->order(['bbs_article.top desc', 'bbs_article.id desc', 'bbs_article.top_time desc'])
                ->whereor('bbs_article.top_time', 'gt', time())
                ->whereor('bbs_article.top_time', 'eq', null)
                ->where("bbs_article.classification_main=$class_id")
                ->where("bbs_article.class_id = $bbs_classification_son_id[id]") //要求分类id 和分类别民的id 一致

                ->where("bbs_article.start = 0 OR bbs_article.start = 3") //阅读权限
                ->join('user', 'bbs_article.user_id=user.id')
                ->count();
        }

        foreach ($list as $key => $value) {
            $list[$key]['count'] = Db::table('bbs_comments')
                ->where(['article_id' => $value['id']])
                ->count();
        }

        $page = getPageHtml($list_count, $bbs_settings['articlelist_list_number'], 1, ['alias' => $alias, 'class_id' => $class_id]);
        $Data['code'] = 1;
        $Data['data'] = $list;
        $Data['page_html'] = $page;
        return $Data;
    }
}
